<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>Quartz | 尼古拉斯-鱼丸博客 | 喝酒、吃肉、不赌</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="SpringBoot,Quartz">
    <meta name="description" content="SpringBoot+Quartz SpringBoot整合Quartz定时任务什么是Quartz?   Quartz是一个开源的作业调度框架，它完全由Java写成，并设计用于J2SE和J2EE应用中。它提供了巨 大的灵 活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征，如：数据库支持，集群，插件，EJB作业预构 建，JavaMail及其它，支持cron-lik">
<meta name="keywords" content="SpringBoot,Quartz">
<meta property="og:type" content="article">
<meta property="og:title" content="Quartz">
<meta property="og:url" content="http://fish_08.oschina.io/2017/06/28/quartz/index.html">
<meta property="og:site_name" content="尼古拉斯-鱼丸博客">
<meta property="og:description" content="SpringBoot+Quartz SpringBoot整合Quartz定时任务什么是Quartz?   Quartz是一个开源的作业调度框架，它完全由Java写成，并设计用于J2SE和J2EE应用中。它提供了巨 大的灵 活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征，如：数据库支持，集群，插件，EJB作业预构 建，JavaMail及其它，支持cron-lik">
<meta property="og:updated_time" content="2017-06-28T08:16:51.563Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Quartz">
<meta name="twitter:description" content="SpringBoot+Quartz SpringBoot整合Quartz定时任务什么是Quartz?   Quartz是一个开源的作业调度框架，它完全由Java写成，并设计用于J2SE和J2EE应用中。它提供了巨 大的灵 活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征，如：数据库支持，集群，插件，EJB作业预构 建，JavaMail及其它，支持cron-lik">
    
        <link rel="alternative" href="/atom.xml" title="尼古拉斯-鱼丸博客" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.6.7">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">于亮</h5>
          <a href="mailto:136214289@qq.com" title="136214289@qq.com" class="mail">136214289@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                文档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://git.oschina.net/fish_08" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://weibo.com/2837335591/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/custom"  >
                <i class="icon icon-lg icon-link"></i>
                测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Quartz</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Quartz</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-06-28T08:16:51.562Z" itemprop="datePublished" class="page-time">
  2017-06-28
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#SpringBoot整合Quartz定时任务"><span class="post-toc-number">1.</span> <span class="post-toc-text">SpringBoot整合Quartz定时任务</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#什么是Quartz"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">什么是Quartz?</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#准备工作，引入依赖"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">准备工作，引入依赖</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#配置quartz和数据库连接"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">配置quartz和数据库连接</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#config包下启动配置"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">config包下启动配置</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#定时任务类"><span class="post-toc-number">1.5.</span> <span class="post-toc-text">定时任务类</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#定时操作工具类"><span class="post-toc-number">1.6.</span> <span class="post-toc-text">定时操作工具类</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#新建一个定时任务"><span class="post-toc-number">1.7.</span> <span class="post-toc-text">新建一个定时任务</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Quartz表的含义"><span class="post-toc-number">1.8.</span> <span class="post-toc-text">Quartz表的含义</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-quartz"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Quartz</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年06月28日 16:16" datetime="2017-06-28T08:16:51.562Z"  itemprop="datePublished">2017-06-28</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>SpringBoot+Quartz</p>
<h2 id="SpringBoot整合Quartz定时任务"><a href="#SpringBoot整合Quartz定时任务" class="headerlink" title="SpringBoot整合Quartz定时任务"></a>SpringBoot整合Quartz定时任务</h2><h3 id="什么是Quartz"><a href="#什么是Quartz" class="headerlink" title="什么是Quartz?"></a>什么是Quartz?</h3><p>   Quartz是一个开源的作业调度框架，它完全由Java写成，并设计用于J2SE和J2EE应用中。它提供了巨 大的灵 活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征，如：数据库支持，集群，插件，EJB作业预构 建，JavaMail及其它，支持cron-like表达式等等。该项目已经被 Terracotta收购。</p>
<p>链接: <a href="http://www.quartz-scheduler.org/" target="_blank" rel="external">Quartz官网</a></p>
<h3 id="准备工作，引入依赖"><a href="#准备工作，引入依赖" class="headerlink" title="准备工作，引入依赖"></a>准备工作，引入依赖</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">&lt;!-- 定时任务jar --&gt;</div><div class="line"> &lt;dependency&gt;</div><div class="line">     &lt;groupId&gt;org.quartz-scheduler&lt;/groupId&gt;</div><div class="line">     &lt;artifactId&gt;quartz&lt;/artifactId&gt;</div><div class="line">     &lt;version&gt;2.2.3&lt;/version&gt;</div><div class="line">&lt;/dependency&gt;</div><div class="line">&lt;!-- 启动异常，数据库连接驱动版本要尽量新,使得数据库版本和驱动版本匹配 --&gt;</div><div class="line">&lt;dependency&gt;</div><div class="line">     &lt;groupId&gt;mysql&lt;/groupId&gt;</div><div class="line">     &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</div><div class="line">     &lt;version&gt;5.1.41&lt;/version&gt;</div><div class="line">&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<h3 id="配置quartz和数据库连接"><a href="#配置quartz和数据库连接" class="headerlink" title="配置quartz和数据库连接"></a>配置quartz和数据库连接</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#quartz配置 quartz.properties</span></div><div class="line">org.quartz.scheduler.instanceName:xxxxxx</div><div class="line">org.quartz.scheduler.instanceId: AUTO</div><div class="line">org.quartz.scheduler.skipUpdateCheck: <span class="literal">true</span></div><div class="line">org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool</div><div class="line">org.quartz.threadPool.threadCount: 5</div><div class="line">org.quartz.threadPool.threadPriority: 5</div><div class="line">org.quartz.jobStore.misfireThreshold: 60000</div><div class="line">org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX  </div><div class="line">org.quartz.jobStore.driverDelegateClass= org.quartz.impl.jdbcjobstore.StdJDBCDelegate</div><div class="line">org.quartz.jobStore.useProperties=<span class="literal">false</span></div><div class="line">org.quartz.jobStore.dataSource=myDS</div><div class="line">org.quartz.jobStore.tablePrefix=QRTZ_</div><div class="line">org.quartz.jobStore.isClustered=<span class="literal">true</span></div><div class="line"></div><div class="line"><span class="comment">#数据库配置  xxx.xml</span></div><div class="line">spring:</div><div class="line">  datasource:</div><div class="line">    url: jdbc:mysql://localhost:3306/xxxxxx?allowMultiQueries=<span class="literal">true</span></div><div class="line">    username: xxxxxx</div><div class="line">    password: xxxxxx</div><div class="line">    driver-class-name: com.mysql.jdbc.Driver</div></pre></td></tr></table></figure>
<h3 id="config包下启动配置"><a href="#config包下启动配置" class="headerlink" title="config包下启动配置"></a>config包下启动配置</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div></pre></td><td class="code"><pre><div class="line">//1.QuartzSpringJobFactory 和 2.QuartzConifgue</div><div class="line">//job是在quartz中实例化出来的 ，把Quartz放入Spring管理</div><div class="line">import org.quartz.spi.TriggerFiredBundle;</div><div class="line">import org.springframework.beans.factory.annotation.Autowired;</div><div class="line">import org.springframework.beans.factory.config.AutowireCapableBeanFactory;</div><div class="line">import org.springframework.scheduling.quartz.AdaptableJobFactory;</div><div class="line"></div><div class="line">public class QuartzSpringJobFactory extends AdaptableJobFactory &#123;</div><div class="line"></div><div class="line">    @Autowired</div><div class="line">    private AutowireCapableBeanFactory capableBeanFactory;</div><div class="line">    </div><div class="line">    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception &#123;</div><div class="line">  </div><div class="line">        Object jobInstance = super.createJobInstance(bundle);</div><div class="line">        capableBeanFactory.autowireBean(jobInstance);</div><div class="line">        <span class="built_in">return</span> jobInstance;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">//Quartz定时任务配置</div><div class="line">import org.springframework.beans.factory.annotation.Qualifier;</div><div class="line">import org.springframework.beans.factory.config.PropertiesFactoryBean;</div><div class="line">import org.springframework.context.annotation.Bean;</div><div class="line">import org.springframework.context.annotation.Configuration;</div><div class="line">import org.springframework.core.io.ClassPathResource;</div><div class="line">import org.springframework.scheduling.quartz.SchedulerFactoryBean;</div><div class="line">import javax.sql.DataSource;</div><div class="line">import java.io.IOException;</div><div class="line">import java.util.Properties;</div><div class="line"></div><div class="line"></div><div class="line">@Configuration</div><div class="line">public class QuartzConifgue &#123;</div><div class="line">    @Bean</div><div class="line">    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource,@Qualifier(<span class="string">"JobFactory"</span>)  QuartzSpringJobFactory jobFactory) throws IOException &#123;</div><div class="line">        SchedulerFactoryBean factory = new SchedulerFactoryBean();</div><div class="line">        factory.setOverwriteExistingJobs(<span class="literal">true</span>);</div><div class="line">        factory.setJobFactory(jobFactory);</div><div class="line">        factory.setDataSource(dataSource);</div><div class="line">	//此属性不填写，会默认SchedulerFactoryBean，用于区分集群不同服务下的定时任务，或者启动服务，会报找不到别的服务的任务类</div><div class="line">        factory.setSchedulerName(<span class="string">"fish_name"</span>);</div><div class="line">        factory.setQuartzProperties(quartzProperties());</div><div class="line">        <span class="built_in">return</span> factory;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Bean</div><div class="line">    public Properties quartzProperties() throws IOException &#123;</div><div class="line">        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();</div><div class="line">        propertiesFactoryBean.setLocation(new ClassPathResource(<span class="string">"quartz.properties"</span>));</div><div class="line">        propertiesFactoryBean.afterPropertiesSet();</div><div class="line">        <span class="built_in">return</span> propertiesFactoryBean.getObject();</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    @Bean(<span class="string">"JobFactory"</span>)</div><div class="line">    public QuartzSpringJobFactory <span class="function"><span class="title">JobFactory</span></span>()&#123;</div><div class="line">    	QuartzSpringJobFactory jobFactory = new QuartzSpringJobFactory();</div><div class="line">    	<span class="built_in">return</span> jobFactory;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="定时任务类"><a href="#定时任务类" class="headerlink" title="定时任务类"></a>定时任务类</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">  public class FishJob implements Job &#123;</div><div class="line">	private Logger logger = LoggerFactory.getLogger(this.getClass());</div><div class="line">	@Override</div><div class="line">	public void execute(JobExecutionContext context)throws JobExecutionException &#123;</div><div class="line">		Long fishJobId = null;</div><div class="line">		try &#123;</div><div class="line">			//获取JobDataMap</div><div class="line">			JobDataMap dataMap = context.getJobDetail().getJobDataMap();</div><div class="line">			fishJobId = (Long) dataMap.get(<span class="string">"fishJobId"</span>); //获取你绑定给此任务map数据的某个key_value</div><div class="line"></div><div class="line">		&#125; catch (Exception e) &#123;</div><div class="line">			logger.error(e);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="定时操作工具类"><a href="#定时操作工具类" class="headerlink" title="定时操作工具类"></a>定时操作工具类</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div></pre></td><td class="code"><pre><div class="line">@Service</div><div class="line">public class QuartzTools &#123;</div><div class="line">	@Autowired</div><div class="line">	private Scheduler scheduler;</div><div class="line">	</div><div class="line">	// 日志处理</div><div class="line">	private Logger logger = LoggerFactory.getLogger(this.getClass());</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 通过表达式追加任务</div><div class="line">	 * @<span class="built_in">return</span></div><div class="line">     * @throws RuntimeExeception</div><div class="line">     */</div><div class="line">	public boolean addJobByCron(String name, String group,Class&lt;? extends Job&gt; jobclazz, String cron,JobDataMap newJobDataMap) throws RuntimeExeception &#123;</div><div class="line">		boolean flag = <span class="literal">false</span>;</div><div class="line">		try &#123;</div><div class="line">			JobDetail jobDetail =null;</div><div class="line">			JobBuilder  jb=	JobBuilder.newJob(jobclazz);</div><div class="line">			jb.setJobData(newJobDataMap);</div><div class="line">			jobDetail=jb.withIdentity(name, group).build(); // 1、创建一个JobDetail实例，指定Quartz</div><div class="line">			// 创建任务触发器</div><div class="line">			Trigger trigger = TriggerBuilder</div><div class="line">					.newTrigger()</div><div class="line">					.withIdentity(name, group)</div><div class="line">					.withSchedule(</div><div class="line">							CronScheduleBuilder.cronSchedule(cron)).build();</div><div class="line">			scheduler.scheduleJob(jobDetail, trigger);</div><div class="line">			flag = <span class="literal">true</span>;</div><div class="line">			logger.info(<span class="string">"创建定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 成功，定时执行表达式：[&#123;&#125;]"</span>, cron);</div><div class="line">		&#125; catch (SchedulerException e) &#123;</div><div class="line">			flag = <span class="literal">false</span>;</div><div class="line">			logger.info(<span class="string">"创建定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 失败，定时执行表达式：[&#123;&#125;]"</span>, cron, e);</div><div class="line">			throw new RuntimeExeception(<span class="string">"fish.addJob"</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">return</span> flag;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	public boolean modifyJobTime(String name, String group, Date date) throws RuntimeExeception  &#123;</div><div class="line">		boolean flag = <span class="literal">false</span>;</div><div class="line">		try &#123;</div><div class="line">			TriggerKey tk = TriggerKey.triggerKey(name, group);</div><div class="line">			// 构造任务触发器</div><div class="line">			Trigger trg = TriggerBuilder</div><div class="line">					.newTrigger()</div><div class="line">					.withIdentity(name, group)</div><div class="line">					.withSchedule(</div><div class="line">							CronScheduleBuilder.cronSchedule(CronDateUtils</div><div class="line">									.getCron(date))).build();</div><div class="line">			scheduler.rescheduleJob(tk, trg);</div><div class="line">			flag = <span class="literal">true</span>;</div><div class="line">			logger.info(<span class="string">"修改定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 成功，修改执行时间：[&#123;&#125;]"</span>, date);</div><div class="line">		&#125; catch (SchedulerException e) &#123;</div><div class="line">			flag = <span class="literal">false</span>;</div><div class="line">			logger.info(<span class="string">"修改定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 失败，修改执行时间：[&#123;&#125;]"</span>, date, e);</div><div class="line">			throw new RuntimeExeception(<span class="string">"fish.modifyJobTime"</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">return</span> flag;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * @throws RuntimeExeception</div><div class="line">	 * @throws</div><div class="line">	 */</div><div class="line">	public boolean parseJob(String name, String group) throws RuntimeExeception &#123;</div><div class="line">		boolean flag = <span class="literal">false</span>;</div><div class="line">		try &#123;</div><div class="line">			JobKey jobKey = JobKey.jobKey(name, group);</div><div class="line">			scheduler.pauseJob(jobKey);</div><div class="line">			flag = <span class="literal">true</span>;</div><div class="line">			logger.info(<span class="string">"暂停定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 成功，暂停执行时间：[&#123;&#125;]"</span>, new Date());</div><div class="line">		&#125; catch (SchedulerException e) &#123;</div><div class="line">			flag = <span class="literal">false</span>;</div><div class="line">			logger.info(<span class="string">"暂停定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 失败，暂停执行时间：[&#123;&#125;]"</span>, new Date(),e);</div><div class="line">			throw new RuntimeExeception(<span class="string">"fish.parseJob"</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">return</span> flag;</div><div class="line">	&#125;</div><div class="line">	/**</div><div class="line">	 * @throws RuntimeExeception</div><div class="line">	* @Title: deleteJob</div><div class="line">	* @throws</div><div class="line">	*/</div><div class="line">	public boolean deleteJob(String name, String group) throws RuntimeExeception &#123;</div><div class="line">		boolean flag = <span class="literal">false</span>;</div><div class="line">		try &#123;</div><div class="line">			JobKey jobKey = JobKey.jobKey(name, group);</div><div class="line">			scheduler.deleteJob(jobKey);</div><div class="line">			flag = <span class="literal">true</span>;</div><div class="line">			logger.info(<span class="string">"删除定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 成功，暂停执行时间：[&#123;&#125;]"</span>, new Date());</div><div class="line">		&#125; catch (SchedulerException e) &#123;</div><div class="line">			flag = <span class="literal">false</span>;</div><div class="line">			logger.info(<span class="string">"删除定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 失败，暂停执行时间：[&#123;&#125;]"</span>, new Date(),e);</div><div class="line">			throw new RuntimeExeception(<span class="string">"fish.deleteJob"</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">return</span> flag;</div><div class="line">	&#125;</div><div class="line">	/**</div><div class="line">	 * @throws RuntimeExeception</div><div class="line">	 * @Title: resumeJob</div><div class="line">	 * @throws</div><div class="line">	 */</div><div class="line">	public boolean resumeJob(String name, String group) throws RuntimeExeception &#123;</div><div class="line">		boolean flag = <span class="literal">false</span>;</div><div class="line">		try &#123;</div><div class="line">			JobKey jobKey = JobKey.jobKey(name, group);</div><div class="line">			scheduler.resumeJob(jobKey);</div><div class="line">			flag = <span class="literal">true</span>;</div><div class="line">			logger.info(<span class="string">"恢复定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 成功，恢复执行时间：[&#123;&#125;]"</span>, new Date());</div><div class="line">		&#125; catch (SchedulerException e) &#123;</div><div class="line">			flag = <span class="literal">false</span>;</div><div class="line">			logger.info(<span class="string">"恢复定时任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 失败，恢复执行时间：[&#123;&#125;]"</span>, new Date(),e);</div><div class="line">			throw new RuntimeExeception(<span class="string">"fish.resumeJob"</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">return</span> flag;</div><div class="line">	&#125;</div><div class="line">	/**</div><div class="line">	 * @throws RuntimeExeception</div><div class="line">	* @Title: isExistJob</div><div class="line">	* @throws</div><div class="line">	*/</div><div class="line">	public boolean isExistJob(String name, String group) throws RuntimeExeception&#123;</div><div class="line">		boolean flag = <span class="literal">false</span>;</div><div class="line">		try &#123;</div><div class="line">			JobKey jobKey = JobKey.jobKey(name, group);</div><div class="line">			flag = scheduler.checkExists(jobKey);</div><div class="line">		&#125; catch (SchedulerException e) &#123;</div><div class="line">			logger.info(<span class="string">"检测任务=&gt; [任务名称："</span> + name + <span class="string">" 任务组："</span> + group</div><div class="line">					+ <span class="string">"] 失败，检测执行时间：[&#123;&#125;]"</span>, new Date(),e);</div><div class="line">			throw new RuntimeExeception(<span class="string">"fish.isExistJob"</span>);</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">return</span> flag;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="新建一个定时任务"><a href="#新建一个定时任务" class="headerlink" title="新建一个定时任务"></a>新建一个定时任务</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"> </div><div class="line">@Autowired</div><div class="line">private QuartzTools quartzTools;// 注入任务调度</div><div class="line"></div><div class="line">@RequestMapping(value=<span class="string">"job"</span>)</div><div class="line">public String <span class="function"><span class="title">Job</span></span>() &#123;</div><div class="line">//每五秒执行一次</div><div class="line">	try &#123;</div><div class="line">		JobDataMap  jobDataMap=new JobDataMap();</div><div class="line">		jobDataMap.put(<span class="string">"fishJobId"</span>, <span class="string">"10"</span>);</div><div class="line">		quartzTools.addJobByCron(<span class="string">"name"</span>,</div><div class="line">                   <span class="string">"group"</span>, FishJob.class,</div><div class="line">                   <span class="string">"0/5 * * * * ? "</span>, jobDataMap);</div><div class="line">	&#125; catch (RuntimeExeception runtimeExeception) &#123;</div><div class="line">		runtimeExeception.printStackTrace();</div><div class="line">	&#125;</div><div class="line">	<span class="built_in">return</span> <span class="string">"Job"</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="Quartz表的含义"><a href="#Quartz表的含义" class="headerlink" title="Quartz表的含义"></a>Quartz表的含义</h3><p>qrtz_blob_triggers : 以Blob 类型存储的触发器。<br>qrtz_calendars：存放日历信息， quartz可配置一个日历来指定一个时间范围。<br>qrtz_cron_triggers：存放cron类型的触发器。<br>qrtz_fired_triggers：存放已触发的触发器。<br>qrtz_job_details：存放一个jobDetail信息。<br>qrtz_job_listeners：job<strong>监听器</strong>。<br>qrtz_locks： 存储程序的悲观锁的信息(假如使用了悲观锁)。<br>qrtz_paused_trigger_graps：存放暂停掉的触发器。<br>qrtz_scheduler_state：调度器状态。<br>qrtz_simple_triggers：简单触发器的信息。<br>qrtz_trigger_listeners：触发器监听器。<br>qrtz_triggers：触发器的基本信息。</p>
<p>cron表达式生成在线工具类:(<a href="http://cron.qqe2.com/" target="_blank" rel="external">http://cron.qqe2.com/</a>)</p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-06-28T08:16:51.563Z" itemprop="dateUpdated">2017年6月28日 16:16</time>
</span><br>


        如要转载请注明出处：<a href="/2017/06/28/quartz/" target="_blank" rel="external">http://fish_08.oschina.io/2017/06/28/quartz/</a>
    </div>
    <footer>
        <a href="http://fish_08.oschina.io">
            <img src="/img/avatar.jpg" alt="于亮">
            于亮
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Quartz/">Quartz</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/SpringBoot/">SpringBoot</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://fish_08.oschina.io/2017/06/28/quartz/&title=《Quartz》 — 尼古拉斯-鱼丸博客&pic=http://fish_08.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://fish_08.oschina.io/2017/06/28/quartz/&title=《Quartz》 — 尼古拉斯-鱼丸博客&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://fish_08.oschina.io/2017/06/28/quartz/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Quartz》 — 尼古拉斯-鱼丸博客&url=http://fish_08.oschina.io/2017/06/28/quartz/&via=http://fish_08.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://fish_08.oschina.io/2017/06/28/quartz/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/07/04/redis/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">redis</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/06/28/mongodb/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">MongoDB</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢包子~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.png" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="http://fish_08.oschina.io/" target="_blank">yuliang</a></span>
            <span>尼古拉斯-鱼丸博客 &copy; 2015 - 2018</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://fish_08.oschina.io/2017/06/28/quartz/&title=《Quartz》 — 尼古拉斯-鱼丸博客&pic=http://fish_08.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://fish_08.oschina.io/2017/06/28/quartz/&title=《Quartz》 — 尼古拉斯-鱼丸博客&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://fish_08.oschina.io/2017/06/28/quartz/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Quartz》 — 尼古拉斯-鱼丸博客&url=http://fish_08.oschina.io/2017/06/28/quartz/&via=http://fish_08.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://fish_08.oschina.io/2017/06/28/quartz/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



</script>

<script src="/js/main.min.js?v=1.6.7"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.6.7" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
